package vesion2.tank;


import vesion2.barrier.Barrier;
import vesion2.main.ObjectManager;
import vesion2.object.Circle;
import vesion2.object.Point;
import vesion2.object.special.Motion;
import vesion2.object.special.Orientation;
import vesion2.tank.BaseTank;
import vesion2.tank.EnemyTank;
import vesion2.tank.MyTank;

import java.awt.*;
import java.io.Serializable;

/**
 * 以Circle为基础的子弹
 */
public class Bullet extends Circle implements Motion{
    private ObjectManager manager;
    private BaseTank tank;
    //0表示自己,1表示敌人
    private int hostType;
    private int speed;
    private int ort;


    private transient BulletThread bulletThread;

    public Bullet(){
        super(new Point(0,0),0);
    }
    public Bullet(BaseTank tank){
        this(tank,20);
        bulletThread=new BulletThread();
    }

    /**
     *
     * @param tank
     * @param speed 平均每100毫秒的移动的总像素
     */
    public Bullet(BaseTank tank, int speed){
        super(new Point(0,0),3);
        this.tank=tank;
        this.speed=speed;
        this.ort= tank.getOrt();
        manager=tank.getManager();
        if(tank instanceof EnemyTank) {
            hostType = 1;
            backColor=new Color(25, 105, 185, 255);
        }
        else {
            hostType = 0;
            backColor=new Color(252, 4, 116, 255);
        }
        int x=tank.getLeft_up().getX();
        int y=tank.getLeft_up().getY();
        switch (ort){
            case Orientation.UP:center.setX(x+20);center.setY(y+radius);break;
            case Orientation.DOWN:center.setX(x+20);center.setY(y+50-radius);break;
            case Orientation.LEFT:center.setX(x+radius);center.setY(y+20);break;
            case Orientation.RIGHT:center.setX(x+50-radius);center.setY(y+20);break;
        }
    }

    public ObjectManager getManager() {
        return manager;
    }

    public BaseTank getTank() {
        return tank;
    }

    public int getHostType() {
        return hostType;
    }

    public void setHostType(int hostType) {
        this.hostType = hostType;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void start(){
        bulletThread.start();
    }

    public void reStart(){
        if(!bulletThread.isAlive()) {
            bulletThread = new BulletThread();
            bulletThread.start();
        }
    }

    @Override
    public void setAlive(boolean alive) {
        isAlive = alive;
        if(!isAlive&&hostType==0)
            ((MyTank)tank).increaseCapacity();
    }

    public int getOrt() {
        return ort;
    }

    public void setOrt(int ort) {
        this.ort = ort;
    }

    @Override
    public boolean canMove() {
        //检查是否到边界
        if (center.getX() - radius < 0 || center.getY() - radius < 0 ||
                center.getX() + radius > manager.getGamePanel().getWidth() ||
                center.getY() + radius > manager.getGamePanel().getHeight())
            return false;

        //检查是否与坦克碰撞
        if (hostType == 1) {
            if (manager.getMyTank().isAlive() && this.isIntersect(manager.getMyTank())) {
                manager.getMyTank().setAlive(false);
                return false;
            }
        } else {
            for (EnemyTank enemyTank : manager.getEnemyTanks())
                if (this.isIntersect(enemyTank)) {
                    enemyTank.setAlive(false);
                    return false;
                }
        }

        //检测障碍物
        for (int i=0;i< manager.getBarriers().size();i++) {
            Barrier barrier=manager.getBarriers().get(i);
            if (barrier.isIntersect(this))
                barrier.collideWithObject(this);
        }
        
        return true;
    }

    @Override
    public boolean move() {
        if(!isAlive)
            return false;
        switch (ort){
            case  Orientation.UP:
                for (int i=0;i<speed;i++) {
                    center.setY(center.getY() - 1);
                    if(!canMove())
                    {
                        center.setY(center.getY() +1);
                        setAlive(false);
                        return false;
                    }
                }
                break;
            case  Orientation.DOWN:
                for (int i=0;i<speed;i++) {
                    center.setY(center.getY() + 1);
                    if(!canMove())
                    {
                        center.setY(center.getY() -1);
                        setAlive(false);
                        return false;
                    }
                }
                break;
            case  Orientation.LEFT:
                for (int i=0;i<speed;i++) {
                    center.setX(center.getX()-1);
                    if(!canMove())
                    {
                        center.setX(center.getX()+1);
                        setAlive(false);
                        return false;
                    }
                }
                break;
            case  Orientation.RIGHT:
                for (int i=0;i<speed;i++) {
                    center.setX(center.getX()+1);
                    if(!canMove())
                    {
                        center.setX(center.getX()-1);
                        setAlive(false);
                        return false;
                    }
                }
                break;
            default:break;
        }
        return true;
    }

    class BulletThread extends Thread{
        public BulletThread(){
            super();
            setName(hostType+"-bullt");
        }

        @Override
        public void run() {
            super.run();
            while (isAlive&&!tank.getManager().getGamePanel().isStop())
            {
                move();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
